<?php

namespace App\Admin\Controllers;

use App\Models\Food;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class FoodController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('食物');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('食物');
            $content->description('修改');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('食物');
            $content->description('新增');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Food::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');
            $grid->model()->where([['is_delete', '=', 0]]);
            $grid->id('ID')->sortable();

            $grid->name('名称');
            $grid->unit('单位')->display(function ($unit) {
               return $unit == 1 ? "<span class=\"label label-info\">g</span>" :"<span class=\"label label-success\">ml</span>";
            });
            $grid->caloric('总热量(Kcal)');
            $grid->fat_caloric('脂肪热量(Kcal)');
            $grid->protein_caloric('蛋白质热量(Kcal)');
            $grid->carbohydrate_caloric('碳水化合物热量(Kcal)');
            $grid->status('状态')->switch([
                'on'  => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
                'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
            ]);
            $grid->created_at('创建时间');

            $grid->disableExport();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });
            $grid->filter(function ($filter) {
                $filter->like('name', '名称');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Food::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->switch('status', '状态')->states([
                'on'  => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
                'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
            ])->default(1);
            $form->text('name', '名称')->rules('required', [
                'required' => '食物名称不能为空'
            ]);
            $form->select('unit', '单位')->options([1 => 'g', 2 => 'ml'])->default(1);
            $form->number('caloric', '总热量(Kcal)')->rules('required',[
                'required' => '热量不能为空',
            ]);
            $form->number('fat_caloric', '脂肪热量(Kcal)')->rules('required',[
                'required' => '肪热量不能为空',
            ]);
            $form->number('protein_caloric', '蛋白质热量(Kcal)')->rules('required',[
                'required' => '蛋白质热量不能为空',
            ]);
            $form->number('carbohydrate_caloric', '碳水化合物热量(Kcal)')->rules('required',[
                'required' => '碳水化合物热量不能为空',
            ]);
        });
    }
}
